package com.fc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fc.entity.LoginUser;
import com.fc.entity.User;
import com.fc.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserDetailServiceImpl implements UserDetailsService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public UserDetails loadUserByUsername(String identifier){
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", identifier).or().eq("phone_number",identifier);
        User user = userMapper.selectOne(queryWrapper);
        // 查询用户角色
        List<String> roles = userMapper.selectRolesByUserId(user.getId());
        return new LoginUser(user, roles);
    }
}
